package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.SkinItem;
import com.andatsoft.laisim.entity.ThemeItem;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.Utils;
import com.nineoldandroids.view.ViewHelper;

public class SkinListDialogAdapter extends ArrayAdapter<ThemeItem> {

	private List<SkinItem> mItemList;
	private LayoutInflater mInflater;
	private Theme mTheme;

	public SkinListDialogAdapter(Context context, List<SkinItem> items) {
		super(context, 0);
		this.mItemList = items;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
	}

	public class ViewHolder {
		public TextView tvName;
		public TextView tvAuthor;
	}

	@Override
	public int getCount() {
		if (mItemList == null)
			return 0;
		return mItemList.size();
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder vHolder;
		if (view == null) {
			view = mInflater.inflate(R.layout.dialog_theme_list_item, parent,
					false);
			vHolder = new ViewHolder();
			vHolder.tvName = (TextView) view
					.findViewById(R.id.tv_theme_list_dialog_name);
			vHolder.tvAuthor = (TextView) view
					.findViewById(R.id.tv_theme_list_dialog_author);
			view.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) view.getTag();
		}
		// Because the view holder is holding the reference of the text
		// view...
		vHolder.tvName.setText(mItemList.get(position).name);
		vHolder.tvAuthor.setText(mItemList.get(position).author);

		// // highlight view selected
		if (mItemList.get(position).isSelected == 1) {
			if (Utils.androidOs < Common.ANDROID_OS_GINGERBREAD_TO_ICS) {
				ViewHelper.setAlpha(view, mTheme.getSelectedItemListAlpha());
			} else {
				view.setAlpha(mTheme.getSelectedItemListAlpha());
			}
		} else {
			if (Utils.androidOs < Common.ANDROID_OS_GINGERBREAD_TO_ICS) {
				ViewHelper.setAlpha(view, mTheme.getNormalItemListAlpha());
			} else {
				view.setAlpha(mTheme.getNormalItemListAlpha());
			}
		}
		return view;
	}

	public void setData(List<SkinItem> items) {
		this.mItemList = items;
		notifyDataSetChanged();
	}

}
